<template>
    <view>
        <!-- pages/login/login.wxml -->
        <form @submit.stop.prevent="formSubmit">
            <view>
                <view class="textArea">
                    <view class="logo">论坛管家</view>
                    <view class="bottomLine">
                        <input class="weui-input" name="username" @change="inputNameChange" :value="username" placeholder="账号" />
                    </view>
                    <view class="bottomLine">
                        <input class="weui-input" type="password" name="password" placeholder="密码" />
                    </view>
                    <view class="bottomLine viewflex" v-if="imageSrc">
                        <input class="weui-input" name="seccodeverify" placeholder="验证码" />
                        <image class="secodeImg" mode="aspectFit" :src="imageSrc" @tap.stop.prevent="downSeccode"></image>
                    </view>
                    <view class="btnArea">
                        <button type="primary" formType="submit">绑 定</button>
                    </view>
<!--                    <view class="register-v">-->
<!--                        <button class="register" open-type="getUserInfo" @getuserinfo="getUserInfo" @tap="toRegister">注 册</button>-->
<!--                        <text class="register-tip">还没有论坛账号?立即</text>-->
<!--                    </view>-->
                </view>
            </view>
        </form>

        <!-- <view class='shareMask' hidden="{{userInfoHidden}}">
    <view class='welcomeContain'>
      <image class='welcomeCover' src='{{minImgDoc}}welcome.png'></image>
      <view class='shareContent'>欢迎来到论坛管家平台，现在就开始享用论坛服务吧</view>
      <button class='welcomeBtn' open-type="getUserInfo"  bindgetuserinfo = "getUserInfo" bindtap='hideWelcome'  id='userInfo'>开始</button>
    </view>
 </view> -->
    </view>
</template>

<script>
// pages/login/login.js
const commonLoginUrl = require('../../config').commonLoginUrl;

const loginmanager = require('../../utils/loginManager');

const minImgDoc = require('../../config').minImgDoc;

const profileUpdateUrl = require('../../config').profileUpdateUrl;

const app = getApp();

export default {
    data() {
        return {
            minImgDoc,
            userInfoHidden: true,
            isRequest: false,
            username: '',
            sechash: '',
            imageSrc: ''
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: function (options) {


        if (loginmanager.openid) {
            if (app.globalData.userInfo) {
                this.username= app.globalData.userInfo.nickName;
            }
        }

        if (app.globalData.uid) {
            loginmanager.loginOut();
        }

        this.downSeccode();
    },
    methods: {
        downSeccode() {
            app.globalData.apimanager.requstSeccode('login').then((res) => {
                if (res.sechash) {
                    this.sechash= res.sechash;this.imageSrc= res.imageSrc;
                }
            });
        },

        formSubmit(e) {
            let param = e.detail.value;

            if (this.sechash) {
                // 有验证码
                param['sechash'] = this.sechash;
            }

            if (loginmanager.openid) {
                param['openid'] = loginmanager.openid;
            }

            if (loginmanager.unionid) {
                param['unionid'] = loginmanager.unionid;
            }

            param['loginsubmit'] = 'yes';
            uni.showLoading({
                title: '登录中',
                icon: 'loading'
            });
            app.globalData.apimanager
                .postRequest(commonLoginUrl, param)
                .then((res) => {
                    uni.hideLoading();

                    if (res.Message.messageval == 'login_succeed') {
                        loginmanager.loginSetUserInfo(res);
                        uni.navigateBack();
                    }

                    uni.showToast({
                        title: res.Message.messagestr,
                        icon: 'none'
                    });
                })
                .catch((res) => {
                    uni.hideLoading();
                    uni.showToast({
                        title: '出错了',
                        icon: 'none'
                    });
                });
        },

        // toRegister() {},

        inputNameChange(e) {
            this.username= e.detail.value;
        },

        getUserInfo: function (e) {
            if (e.detail.userInfo) {
                app.globalData.userInfo = e.detail.userInfo;
                var data = {
                    avatarUrl: e.detail.userInfo.avatarUrl
                };
                this.userInfoHidden= true;

                if (!this.username) {
                    this.username= e.detail.userInfo.nickName;
                }
            } else {
                uni.showToast({
                    title: '为了您更好的体验,请先同意授权',
                    icon: 'none',
                    duration: app.globalData.duration
                });
            }

            uni.navigateTo({
                url: '../register/register'
            });
        }
    }
};
</script>
<style>
@import './login.css';
</style>
